Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Join Pattern #3172

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Added Join Pattern #3172

wants to merge 7 commits into from

Conversation

keshavMM004
Copy link

Implemented the join design pattern that allows multiple threads to be synchronized such that all DemoThreads must complete before any DependentThreads can proceed .

Close #70

Copy link

github-actions bot commented Jan 12, 2025

PR Summary

This PR implements the Join design pattern, enabling synchronization of multiple threads. All DemoThreads must complete before DependentThreads can start. It includes comprehensive documentation, diagrams, and unit tests.

Changes

File Summary
join/README.md This file provides a comprehensive guide to the Java Join Pattern, including its intent, real-world examples, programmatic implementation, usage scenarios, benefits, trade-offs, and related patterns. It also includes a detailed explanation with code examples and program output.
join/etc/JoinPatternFlowDiagram.png New file: Flow diagram illustrating the Join Pattern.
join/etc/java-join-method.png New file: Image illustrating the Java join method.
join/etc/join.urm.puml New file: UML diagram depicting the classes and relationships in the Join Pattern implementation.
join/pom.xml This file configures the Maven project, defining dependencies for JUnit and Mockito for testing purposes. It also sets up the main class for execution.
join/src/main/java/com/iluwatar/join/DemoThread.java Implements Runnable. DemoThreads execute sequentially based on executionOrder, waiting for the preceding thread using join(). Logs start and end times.
join/src/main/java/com/iluwatar/join/DependentThread.java Implements Runnable. DependentThreads start after all DemoThreads complete. Logs start and end times.
join/src/main/java/com/iluwatar/join/JoinPattern.java Uses CountDownLatch to synchronize threads. await() blocks until all DemoThreads finish via countdown(). Manages thread execution order.
join/src/main/java/com/iluwatar/join/JoinPatternDemo.java The main class demonstrates the Join Pattern. It creates and starts DemoThreads and DependentThreads, ensuring proper synchronization.
join/src/test/java/com/iluwatar/join/JoinPatternTest.java Unit test verifying that DemoThreads execute in the specified order using assertArrayEquals.
pom.xml The join module was added to the project's module list.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (7)
Files Processed (11)
  • join/README.md (1 hunk)
  • join/etc/JoinPatternFlowDiagram.png (0 hunks)
  • join/etc/java-join-method.png (0 hunks)
  • join/etc/join.urm.puml (1 hunk)
  • join/pom.xml (1 hunk)
  • join/src/main/java/com/iluwatar/join/DemoThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/DependentThread.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPattern.java (1 hunk)
  • join/src/main/java/com/iluwatar/join/JoinPatternDemo.java (1 hunk)
  • join/src/test/java/com/iluwatar/join/JoinPatternTest.java (1 hunk)
  • pom.xml (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
3 Security Hotspots
48.4% Coverage on New Code (required ≥ 80%)
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Comment on lines +42 to +50
package com.iluwatar.join;

import lombok.extern.slf4j.Slf4j;

/** Here main thread will execute after completion of 4 demo threads
* main thread will continue when CountDownLatch count becomes 0
* CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
* everytime when they will finish .
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The programmatic example in README.md should be minimalistic. It doesn't have to compile. The main purpose is to explain the reader how the pattern works using code snippets.

try {
previous.join();
} catch (InterruptedException e) {
e.printStackTrace();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log to Slf4j log, not to console, e.g.

log.error("An error occurred: {}", e.getMessage(), e);

Comment on lines +29 to +33
/** Here main thread will execute after completion of 4 demo threads
* main thread will continue when CountDownLatch count becomes 0
* CountDownLatch will start with count 4 and 4 demo threads will decrease it by 1
* everytime when they will finish .
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the pattern briefly. After that explain how the code example implements it. Add comments to the code where necessary. Remember, this is material for studying design patterns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Join pattern
2 participants